package com.hr.controller;

import com.hr.entity.LoginUser;
import com.hr.service.LoginService;
import com.hr.utils.ResultInfo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author hkp
 **/

@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody Map<String,Object> map){
        String username = (String) map.get("username");
        String uPassword = (String) map.get("uPassword");
        String uPosition = (String) map.get("uPosition");

        if(StringUtils.isNullOrEmpty(username) || StringUtils.isNullOrEmpty(uPassword)) {
            return ResultInfo.fail("用户名或密码不能为空");
        }
        LoginUser user = loginService.login(username,uPassword,uPosition);
        if(user == null)
            return ResultInfo.fail("用户名或密码错误");
        else {
            return ResultInfo.success("登陆成功").put("user",user);
        }
    }
}
